<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <span>筛选搜索</span>
        <el-button
          @click="handleResetSearch()"
          style="float: right"
          size="small"
          icon="el-icon-delete"
          >重置</el-button
        >
        <el-button
          @click="getList(1)"
          style="float: right; margin-right: 15px"
          type="primary"
          size="small"
          icon="el-icon-search"
          >查询</el-button
        >
      </div>

      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="searchPeopleList"
          size="small"
          label-width="140px"
        >
          <el-form-item label="姓名:">
            <el-input
              style="width: 203px"
              v-model="searchPeopleList.name"
              placeholder="请输入姓名"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="账号状态:">
            <el-select
              v-model="searchPeopleList.status"
              style="width: 203px"
              placeholder="请选择账号状态"
              clearable
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="联系方式:">
            <el-input
              style="width: 203px"
              v-model="searchPeopleList.phone"
              placeholder="请输入联系方式"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="邮箱:">
            <el-input
              style="width: 203px"
              v-model="searchPeopleList.email"
              placeholder="请输入邮箱"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <div style="margin-top: 15px" class="table-container">
      <el-table
        :data="PeopleList"
        v-loading="listLoading"
        style="width: 100%"
        height="320px"
        border
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="用户ID" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.customerId }}</template>
        </el-table-column>
        <el-table-column label="姓名" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="邮箱" width="250" align="center">
          <template slot-scope="scope">{{ scope.row.email }}</template>
        </el-table-column>
        <el-table-column label="性别" width="134" align="center">
          <template slot-scope="scope">
            <p v-if="scope.row.sex == 'SECRET'">保密</p>
            <p v-else-if="scope.row.sex == 'MAN'">男</p>
            <p v-else-if="scope.row.sex == 'WOMAN'">女</p>
            <p v-else>其他</p>
          </template>
        </el-table-column>
        <el-table-column label="出生日期" width="250" align="center">
          <template slot-scope="scope">{{ scope.row.birth }}</template>
        </el-table-column>
        <el-table-column label="联系方式" width="250" align="center">
          <template slot-scope="scope">{{ scope.row.phone }}</template>
        </el-table-column>
        <el-table-column label="家庭住址" width="250" align="center">
          <template slot-scope="scope">{{ scope.row.address }}</template>
        </el-table-column>
        <el-table-column label="账号状态" width="134" align="center">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.status == 'BAN'"
              >禁用</el-tag
            >
            <el-tag type="success" v-else-if="scope.row.status == 'NORMAL'"
              >正常</el-tag
            >
            <el-tag v-else>其他</el-tag></template
          >
        </el-table-column>
        <el-table-column label="创建时间" width="250" align="center">
          <template slot-scope="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column label="更新时间" width="250" align="center">
          <template slot-scope="scope">{{ scope.row.updateTime }}</template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="90" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status == 'NORMAL'"
              @click="handleBan(scope.row.email)"
              size="mini"
              type="warning"
              >禁用</el-button
            >
            <el-button
              v-if="scope.row.status == 'BAN'"
              @click="handleCancleBan(scope.row.email)"
              size="mini"
              type="primary"
              >解禁</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination layout="prev, pager, next" :page-count="pageCunt" @current-change="getList">
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "userManage",
  data() {
    return {
      searchPeopleList: {
        name: "",
        status: "",
        phone: "",
        email: "",
        pageNum: "1",
        pageSize: "5",
      },
      currentPeopleList: {
        name: "",
        status: "",
        sex: "",
        phone: "",
        email: "",
        pageNum: "1",
        pageSize: "5",
      },
      PeopleList: [],
      statusOptions: [
        {
          value: "BAN",
          label: "禁用",
        },
        {
          value: "NORMAL",
          label: "正常",
        },
      ],
      pageCunt: 1,
      listLoading: true,
    };
  },
  created() {
    this.getList(1);
  },
  methods: {
    getList(pageNum) {
      this.currentPeopleList = this.searchPeopleList
      this.currentPeopleList.pageNum = pageNum
      this.listLoading = true;
      this.axios
        .get("/api/sys/auth/cususer/list", {
          params: this.currentPeopleList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.PeopleList = response.data.value.data;
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleResetSearch() {
      this.searchPeopleList = {
        name: "",
        status: "",
        phone: "",
        email: "",
        pageNum: "1",
        pageSize: "5",
      };
    },
    handleSearchList() {
      this.listLoading = true;
      this.axios
        .get("/api/sys/auth/cususer/list", {
          params: this.searchPeopleList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.PeopleList = response.data.value.data;
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleBan(banEmail) {
      this.$confirm("请再次确认是否禁用该用户")
        .then((_) => {
          this.axios
            .post(
              "api/sys/auth/cususer/update?email=" +
                banEmail +
                "&status=" +
                "BAN"
            )
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "禁用成功",
                  type: "success",
                });
                this.getList(1);
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error("禁用失败");
            });
        })
        .catch((_) => {});
    },
    handleCancleBan(banEmail) {
      this.$confirm("请再次确认是否解除禁用该用户")
        .then((_) => {
          this.axios
            .post(
              "api/sys/auth/cususer/update?email=" +
                banEmail +
                "&status=" +
                "NORMAL"
            )
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "解除禁用成功",
                  type: "success",
                });
                this.getList(1);
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error("解除禁用失败");
            });
        })
        .catch((_) => {});
    },
  },
};
</script>
<style></style>